package org.example.teacher.controller;
//专用处理商品控制器

import org.example.teacher.Service.TeacherService;
import org.example.teacher.entity.teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TeacherService teacherService;
//    1.展示数据库中已有的书籍给前端
    @RequestMapping("/teacherList")
    public String teacherList(Model model){
//        1.调用service方法查询到书籍结果
        List<teacher> teacherList = teacherService.findAll();
//        2.将查询结果用model绑定
        model.addAttribute("data", teacherList);
//        3.return视图
        return "teacher/teacherList";
    }
//    2.前端实现增加，删除，更改操作这里处理
    @RequestMapping("/teacherAdd")
    public String teacheraAdd(){
        return "teacher/teacherAdd";
    }

    @RequestMapping("/teacherAddProcess")
    public String teacherAddProcess(@RequestParam String name, @RequestParam String age){
//        1.组装成books实体
        teacher teacher = new teacher(null,name,age);
//        2.调用service的add方法
        if(teacherService.add(teacher)==1){
            return "redirect:/teacher/teacherList";
        }else{
            return "redirect:/teacher/teacherList";
        }
    }

//  处理更新书籍，首先给出一个页面显示该条记录的数据
    @RequestMapping("/teacherUpdate")
    public String teacherUpdate(@RequestParam String id, Model model){
//        首先根据id查询到该条记录信息
        teacher teacher = new teacher();
        teacher.setId(Integer.valueOf(id));
        teacher teacherdb = teacherService.findone(teacher);
        model.addAttribute("data",teacherdb);
        return "teacher/teacherUpdate";
    }

}
